package utils

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
	"os"
)


// GenRSAPubAndPri  bits: 秘钥，filepath：文件路径
func GenRSAPubAndPri(bits int,filepath string ) error {
	_, err := os.Stat(filepath)
	if err != nil && os.IsNotExist(err) { // 目录不存在
		_ = os.Mkdir(filepath, os.ModePerm) // 创建目录
	}

	// 生成私钥文件
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		return err
	}
	derStream := x509.MarshalPKCS1PrivateKey(privateKey)
	priBlock := &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: derStream,
	}

	err= ioutil.WriteFile(filepath+"/private.pem",pem.EncodeToMemory(priBlock), 0644)
	if err!=nil{
		return err
	}
	fmt.Println("=======私钥文件创建成功========")
	// 生成公钥文件
	publicKey := &privateKey.PublicKey
	derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
	if err != nil {
		return err
	}
	publicBlock := &pem.Block{
		Type:  "PUBLIC KEY",
		Bytes: derPkix,
	}

	err= ioutil.WriteFile(filepath+"/public.pem",pem.EncodeToMemory(publicBlock), 0644)
	if err!=nil{
		return err
	}
	fmt.Println("=======公钥文件创建成功=========")

	return nil
}